/*
 * fImageButton.cpp
 *
 *  Created on: 2011-3-17
 *      Author: wangchao
 */

#include "fImageButton.h"

fImageButton* fImageButton::NewLC( fEngine* aEngine )
{
    fImageButton* self = new ( ELeave ) fImageButton();
    CleanupStack::PushL( self );
    self->ConstructL( aEngine );
    return self;
}

fImageButton* fImageButton::NewL( fEngine* aEngine )
{
    fImageButton* self = fImageButton::NewLC( aEngine );
    CleanupStack::Pop(); // self;
    return self;
}

fImageButton::fImageButton()
{

}

fImageButton::~fImageButton()
{

}

void fImageButton::ConstructL( fEngine* aEngine )
{
    BaseInit( aEngine );

}

void fImageButton::RenderFrame()
{
    if( !_layout )
    {
        layout();
    }

    if( _state == EPushed )
    {
        if( _forePushed )
        {
            iGfx->DrawTransImage( Left() + _ptPosition.iX, Top() + _ptPosition.iY, _forePushed, 0 );
        }
        else
        {
            iGfx->DrawRect( Left(), Top(), Right(), Bottom(), 0xcdcdcd, 1 );
        }
    }
    else if( _state == EHilite )
    {
        if( _foreHilite )
        {
            iGfx->DrawTransImage( Left() + _ptPosition.iX, Top() + _ptPosition.iY, _foreHilite, 0 );
        }
        else
        {
            iGfx->DrawRect( Left(), Top(), Right(), Bottom(), 0x7f7f7f, 1 );
        }
    }
    else
    {
        if( _foreNormal )
        {
            iGfx->DrawTransImage( Left() + _ptPosition.iX, Top() + _ptPosition.iY, _foreNormal, 0 );
        }
        else
        {
            iGfx->DrawRect( Left(), Top(), Right(), Bottom(), 0xefefef, 1 );
        }
    }
}

void fImageButton::HandleKey( f_KEY_DES* /*aKeyDes*/)
{

}

TInt fImageButton::HandleMouse( fMouseEvent nType, TPoint sPoint )
{
    if( !PtInCtrl( sPoint ) )
    {
        _state = ENormal;
        return 0;
    }

    switch( nType )
    {
        case f_MOUSEEVENT_PUSHED:
            {
                _state = EPushed;
            }
            break;
        case f_MOUSEEVENT_POPED:
            {
                if( _state == EPushed )
                {
                    if( _observer )
                    {
                        _observer->ImageButtonClicked( this );
                    }
                }
                _state = ENormal;
            }
            break;
        case f_MOUSEEVENT_PUSHMOVE:
            {
                _state = EHilite;
            }
            break;
        default:
            break;
    }
    return 1;
}

void fImageButton::layout()
{
    _layout = true;
    int imageHeight = 0;
    int imageWidth = 0;
    if( _foreNormal )
    {
        imageHeight = _foreNormal->Height();
        imageWidth = _foreNormal->Width();
    }
    else if( _foreHilite )
    {
        imageHeight = _foreHilite->Height();
        imageWidth = _foreHilite->Width();
    }
    else
    {

    }

    _ptPosition.iX = ( Width() - imageWidth ) >> 1;
    _ptPosition.iY = ( Height() - imageHeight ) >> 1;
}
